package auth;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import lib_model.Account.Account;

/**
 * Servlet implementation class Login
 */
@WebServlet("/auth/Login")
public class Login extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public Login() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		System.out.println("get");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String usern = request.getParameter("txtUsername");
		String pw = request.getParameter("txtPassword");
		Account check = new Account();
		check.username = usern;
		check = ((Account) check.GetInformation().ObjResult);
		RequestDispatcher dis = request
				.getRequestDispatcher("../pages/login.jsp");
		if (check != null) {
			if (check.password.equals(pw)) {
				HttpSession s = request.getSession();
				s.setAttribute("User", check);
				dis = request.getRequestDispatcher("../pages/home.jsp");// ok
			} else {
				request.setAttribute("Error", "Password is fail");
				dis = request.getRequestDispatcher("../pages/login.jsp");// fail-pass
			}
		} else {
			request.setAttribute("Error", "Username isn't exist");
			dis = request.getRequestDispatcher("../pages/login.jsp");// usn-not-exist
		}
		dis.forward(request, response);
	}

}
